<!--
 * @Description: 所属周下拉选择
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2023-03-21 10:31:17
 * @LastEditTime: 2023-03-22 16:17:50
 * @FilePath: \cscec-pms-admin\src\components\Select\SelectOfWeek.vue
-->
<template>
  <div>
    <a-select
    v-model="currentValue"
    :placeholder="placeholder"
    v-bind="$attrs"
    v-on="$listeners"
    @popupScroll="popupScroll"
  >
    <a-select-option
      v-for="(item,index) in list"
      :key="index"
      :value="item"
    >
      {{ item }}
    </a-select-option>
  </a-select>
  </div>
</template>
<script>
import weekPlanApi from '@/api/construction/weekPlanApi'
import debounce from 'lodash/debounce'
export default {
  name: 'SelectOfWeek',
  props: {
    // 值
    value: {
      type: [String,Number]
    },
    // 提示语
    placeholder: {
      type: String,
      default: () => '请选择'
    },
    // 数据接口
    loadApi: {
      type: Function,
      default: weekPlanApi.belongWeekSelect
    }
  },
  components: {},
  data() {
    return {
      // 下拉列表
      list: [],
      // 分页页码
      pageNo: 1,
      // 分页每页数量
      pageSize: 10,
      // 总页数
      totalPages: 0,
      // 数据页
      dataNo: 0
    }
  },
  computed:{
    // 值绑定
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('change', value)
      }
    }
  },
  mounted() {
    this.getData()
    
  },
  methods: {
    /**
     * @description: 获取下拉列表
     * @return {void}
     */
    async getData() {
      try {
        const res = await this.loadApi({ pageNo: this.pageNo, pageSize: this.pageSize })
        if (res.code === this.SUCCESS_CODE) {
          if(this.dataNo < res.totalPage){
            this.list = this.list.concat(res.data)
            this.dataNo ++
          }
          if(this.dataNo === 1){
            if(this.list.length > 0){
              this.currentValue =  this.list[0]
              // 首次加载完成且有默认值回调
              this.$emit('loadWeek',this.list[0])
            }
          }
          this.totalPages = res.totalPage
        } else {
          this.$message.warning('获取所属周失败')
        }
      } catch (error) {
        console.log(error)
        this.$message.warning('获取所属周失败')
      }
    },
    /**
     * @description: 获取数据防抖
     * @param {void}
     * @return {function}
     */
    getDataDebounce: debounce(function () {
      this.getData()
    }, 300),
    /**
     * @description: 下拉滚动事件
     * @param {object} e Event事件对象
     * @return {void}
     */    
    popupScroll(e){
      const { target } = e
      let total = target.scrollTop + target.offsetHeight
      let scrollHeight = target.scrollHeight
      if (total >= scrollHeight - 3 && total < scrollHeight + 3 && this.pageNo <= this.totalPages) {
        this.pageNo ++
        this.getDataDebounce()
      }
    }
    
  }
}
</script>
<style lang="less" scoped>
</style>
